
#region copyright notice
/* ====================================================================
 * The UncleGED Open Source Software License
 *
 * Copyright (c) 1999-2009 D. M. DeBacker. All rights reserved.
 * http://gatheringleaves.org/uged
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Products derived from this software may not be called "UncleGED",
 *    nor may "UncleGED" appear in their name, without prior written
 *    permission of D. M. DeBacker.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL D. M. DEBACKER BE LIABLE FOR ANY 
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

namespace UncleGED.Documentation
{
   internal class HtmlHelp
   {
      private FileStream fsPrj;
      private StreamWriter swPrj;

      private FileStream fsToc;
      private StreamWriter swToc;

      private FileStream fsIdx;
      private StreamWriter swIdx;
     
      private string mvarDirectoryName;
      private string mvarProjectName;
      private string mvarDefaultTopic;
      private string mvarHtmlHelpCompiler;
      private bool mvarIncludeFavorites;

      private const int ERROR_FILE_NOT_FOUND = 2;
      private const int ERROR_ACCESS_DENIED = 5;

      public bool IncludeFavorites
      {
         get
         {
            return mvarIncludeFavorites;
         }
         set
         {
            mvarIncludeFavorites = value;
         }
      }
      public string HtmlHelpCompiler
      {
         get
         {
            return mvarHtmlHelpCompiler;
         }
         set
         {
            mvarHtmlHelpCompiler = value;
         }
      }
      public string DefaultTopic
      {
         get
         {
            return mvarDefaultTopic;
         }
         set
         {
            mvarDefaultTopic = value;
         }
      }
      public string ProjectName
      {
         get
         {
            return mvarProjectName;
         }
         set
         {
            mvarProjectName = value;
         }
      }
      public string DirectoryName
      {
         get
         {
            return mvarDirectoryName;
         }
         set
         {
            mvarDirectoryName = value;
         }
      }
      public void SetNewProject(string sDirectoryName, string sProjectName, string sDefaultTopic, string sHtmlHelpCompiler)
      {
         mvarDirectoryName = sDirectoryName;
         mvarProjectName = sProjectName;
         mvarDefaultTopic = sDefaultTopic;
         mvarHtmlHelpCompiler = sHtmlHelpCompiler;
      }
      private string ProjectFilename()
      {
         return mvarProjectName + ".hhp";
      }
      private string ContentsFilename()
      {
         return mvarProjectName + ".hhc";
      }
      private string IndexFilename()
      {
         return mvarProjectName + ".hhk";
      }
      private string LogFilename()
      {
         return mvarProjectName + ".log";
      }
      private string CompiledHtmlFilename()
      {
         return mvarProjectName + ".chm";
      }
      private string PathToProjectFile()
      {
         return mvarDirectoryName + "\\" + ProjectFilename();
      }
      private string PathToContentsFile()
      {
         return mvarDirectoryName + "\\" + ContentsFilename();
      }
      private string PathToIndexFile()
      {
         return mvarDirectoryName + "\\" + IndexFilename();
      }
      private string PathToLogFile()
      {
         return mvarDirectoryName + "\\" + LogFilename();
      }
      public string PathToCompiledHtmlFile()
      {
         return mvarDirectoryName + "\\" + CompiledHtmlFilename();
      }

      public void OpenProjectFile()
      {
         string options = null;
         
         fsPrj = new FileStream(PathToProjectFile(), FileMode.Create);
         swPrj = new StreamWriter(fsPrj);

         if (mvarIncludeFavorites)
            options = "0x63520,220,0x383e,[86,51,872,558],,,,,,,0";
         else
            options = "0x62520,220,0x383e,[86,51,872,558],,,,,,,0";
         
         string Sections = null;
         Sections = "[OPTIONS]" + System.Environment.NewLine;
         Sections = Sections + "Auto Index=Yes" + System.Environment.NewLine;
         Sections = Sections + "Compatibility=1.1 or later" + System.Environment.NewLine;
         Sections = Sections + "Compiled file=" + CompiledHtmlFilename() + System.Environment.NewLine;
         Sections = Sections + "Contents file=" + ContentsFilename() + System.Environment.NewLine;
         Sections = Sections + "Default Window=MsdnHelp" + System.Environment.NewLine;
         Sections = Sections + "Default topic=" + DefaultTopic + System.Environment.NewLine;
         Sections = Sections + "Display compile progress=No" + System.Environment.NewLine;
         Sections = Sections + "Error log file=" + LogFilename() + System.Environment.NewLine;
         Sections = Sections + "Full-text search=Yes" + System.Environment.NewLine;
         Sections = Sections + "Index file=" + IndexFilename() + System.Environment.NewLine;
         Sections = Sections + "Language=0x409 English (United States)" + System.Environment.NewLine + System.Environment.NewLine;
         Sections = Sections + "[WINDOWS]" + System.Environment.NewLine;
         Sections = Sections + "MsdnHelp=" + (char)(34) + mvarProjectName + " Compiled Genealogy" + (char)(34) + ", " + (char)(34) + ContentsFilename() + (char)(34) + ", " + (char)(34) + IndexFilename() + (char)(34) + ", , , , , , , " + options + System.Environment.NewLine + System.Environment.NewLine;
         Sections = Sections + "[Files]" + System.Environment.NewLine;
         
         swPrj.WriteLine(Sections);
      }
      public void AddFileToProject(string filename)
      {
         swPrj.WriteLine(filename + System.Environment.NewLine);
      }
      public void CloseProjectFile()
      {
         swPrj.Close();
      }
      public void OpenContentsFile()
      {
         char quote = (char)(34);
         
         fsToc = new FileStream(PathToContentsFile(), FileMode.Create);
         swToc = new StreamWriter(fsToc);

			swToc.WriteLine("<!DOCTYPE HTML PUBLIC " + quote + "-//IETF//DTD HTML//EN" + quote + ">");
			swToc.WriteLine("<HTML>");
         swToc.WriteLine("<head>");
         swToc.WriteLine("<meta name=" + quote + "GENERATOR" + quote + " content=" + quote + "Microsoft&reg; HTML Help Workshop 4.1" + quote + ">");
         swToc.WriteLine("<!-- Sitemap 1.0 -->");
         swToc.WriteLine("</HEAD><BODY>");
         swToc.WriteLine("<UL>");
      }
      public void AddFileToContents(string headingName, string htmlFilename)
      {
         char quote = (char)(34);
         swToc.WriteLine("\t" + "<LI><OBJECT type=" + quote + "text/sitemap" + quote + ">");
         swToc.WriteLine("\t" + "\t" + "<param name=" + quote + "Name" + quote + " value=" + quote + headingName + quote + ">");
         swToc.WriteLine("\t" + "\t" + "<param name=" + quote + "Local" + quote + " value=" + quote + htmlFilename + quote + ">");
         swToc.WriteLine("\t" + "\t" + "</OBJECT>");
      }
      public void CloseContentsFile()
      {
         swToc.WriteLine("</UL>");
         swToc.WriteLine("</BODY></HTML>");
         swToc.Close();
      }
      public void OpenIndexFile()
      {
         char quote = (char)(34);
         
         fsIdx = new FileStream(PathToIndexFile(), FileMode.Create);
         swIdx = new StreamWriter(fsIdx);

         swIdx.WriteLine("<!DOCTYPE HTML PUBLIC " + quote + "-//IETF//DTD HTML//EN" + quote + ">");
         swIdx.WriteLine("<HTML>");
         swIdx.WriteLine("<HEAD>");
         swIdx.WriteLine("<meta name=" + quote + "GENERATOR" + quote + " content=" + quote + "Microsoft&reg; HTML Help Workshop 4.1" + quote + ">");
         swIdx.WriteLine("<!-- Sitemap 1.0 -->");
         swIdx.WriteLine("</HEAD><BODY>");
         swIdx.WriteLine("<UL>");
      }
      public void CloseIndexFile()
      {
         swIdx.WriteLine("</UL>");
         swIdx.WriteLine("</BODY></HTML>");
         swIdx.Close();
      }
      public void AddFileToIndex(string headingName, string htmlFilename)
      {
         char quote = (char)(34);
         swIdx.WriteLine("\t" + "<LI><OBJECT type=" + quote + "text/sitemap" + quote + ">");
         swIdx.WriteLine("\t" + "\t" + "<param name=" + quote + "Name" + quote + " value=" + quote + headingName + quote + ">");
         swIdx.WriteLine("\t" + "\t" + "<param name=" + quote + "Local" + quote + " value=" + quote + htmlFilename + quote + ">");
         swIdx.WriteLine("\t" + "\t" + "</OBJECT>");
      }

      public void CompileProject(ref string ErrMsg)
      {

         Process myProcess = new Process();

         try
         {

            myProcess.StartInfo.FileName = (char)(34) + mvarHtmlHelpCompiler + (char)(34);
            myProcess.StartInfo.Arguments = (char)(34) + PathToProjectFile() + (char)(34);
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
         }
         catch (Win32Exception ex)
         {
            if (ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
            {
               ErrMsg = ex.Message;
            }

            else if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
            {
               ErrMsg = ex.Message;
            }
         }
         catch (Exception ex1)
         {
            ErrMsg = ex1.Message;
         }
      }

   } //end of class
}
